"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/11/4 15:06
@Description : 

"""
from prettytable import PrettyTable


# 学生类
class Student:
    def __init__(self, student_no, name, age, score, clazz):
        self.student_no = student_no
        self.name = name
        self.age = age
        self.score = score
        self.clazz = clazz

    def __str__(self):
        return f'{self.student_no} {self.name} {self.age} {self.score} {self.clazz}'


#  学生信息操作工具类
class StudentsUtil:

    @staticmethod
    def student_list(students: list[Student]):
        if students:
            for student in students:
                table.add_row(
                    [student.student_no, student.name, student.age, student.score, student.clazz])
            print(table)
            #   清空表格数据行
            table.clear_rows()
        else:
            print('学生信息列表为空,请添加后再查看...')

    @staticmethod
    def get_one_student(student_no: str, students: list[Student]):
        if students:
            for student in students:
                if student.student_no == student_no:
                    table.add_row(
                        [student.student_no, student.name, student.age, student.score, student.clazz])
                    print(table)
                    table.clear_rows()
                    break  # 查询到学生信息后 提前结束循环
            else:
                print(f'{student_no}学号不存在...')
        else:
            print('学生信息列表为空,请添加后再查看...')

    @staticmethod
    def add_student(student: Student, students: list[Student]):
        students.append(student)
        print(f'{student}信息添加完成...')

    @staticmethod
    def student_no_exist(student_no: str, students: list[Student]):
        for student in students:
            if student['student_no'] == student_no:
                return True

        return False

    @staticmethod
    def edit_student(student: Student, students: list[Student]):
        if students:
            for stu in students:
                if stu.student_no == student.student_no:
                    if stu.name:
                        stu.name = student.name
                    if stu.age:
                        stu.age = student.age
                    if stu.score:
                        stu.score = student.score
                    if stu.clazz:
                        stu.clazz = student.clazz
                    break
            else:
                print(f'{student}学生信息不存在')
        else:
            print('学生信息列表为空,请添加后再修改...')

    def remove_one_student(student_no: str, students: list[Student]):
        if students:
            for stu in students:
                if stu.student_no == student_no:
                    students.remove(student)
                    print(f'{student_no}学生信息删除成功')
                    break
            else:
                print(f'{student}不存在,删除失败')
        else:
            print('学生信息列表为空,请添加后再删除...')


table = PrettyTable()
# 设置表头
table.field_names = ['学号', '姓名', '年龄', '分数', '班级']
#  欢迎信息
welcome_info = """
********************欢迎使用学生信息管理系统*************************
         1. 学生列表
         2. 查询学生
         3. 添加学生
         4. 修改学生
         5. 删除学生
         6. 退出程序
********************************************************************
"""
# 定义列表 存储多个学生信息
students = []

while True:
    # 打印功能清单
    print(welcome_info)
    # 功能选择
    user_choice = input('请输入功能序号:')
    match user_choice:
        case '1':
            print('*' * 20, '学生列表', '*' * 20)
            StudentsUtil.student_list(students)
        case '2':
            print('*' * 20, '查询学生', '*' * 20)
            student_no = input('请输入学号:')
            StudentsUtil.get_one_student(student_no, students)
        case '3':
            print('*' * 20, '添加学生', '*' * 20)
            student_no = input('请输入学号:')
            is_exist = StudentsUtil.student_no_exist(student_no, students)
            if is_exist:
                print('学号已存在...')
            else:
                name = input('请输入姓名:')
                age = input('请输入年龄:')
                score = input('请输入分数:')
                clazz = input('请输入班级:')
                student = Student(student_no, name, age, score, clazz)
                students.append(student)
        case '4':
            print('*' * 20, '修改学生', '*' * 20)
            student_no = input('请输入学号:')
            is_exist = StudentsUtil.student_no_exist(student_no, students)
            if is_exist:
                edit_student = Student(student_no, None, None, None, None, )
                # 开始修改学生信息
                is_change = input('修改姓名【y/n】:')
                if is_change == 'y':
                    name = input('请输入姓名:')
                    edit_student.name = name
                is_change = input('修改年龄【y/n】:')
                if is_change == 'y':
                    age = input('请输入年龄:')
                    edit_student.age = int(age)
                is_change = input('修改分数【y/n】:')
                if is_change == 'y':
                    score = input('请输入分数:')
                    edit_student.score = int(score)
                is_change = input('修改班级【y/n】:')
                if is_change == 'y':
                    clazz = input('请输入班级:')
                    edit_student.clazz = clazz
                StudentsUtil.edit_student(edit_student, students)
                print(f'{edit_student}信息更新成功')
                break
            else:
                print(f'{student_no}学号不存在')

        case '5':
            print('*' * 20, '删除学生', '*' * 20)
            student_no = input('请输入学号:')
            StudentsUtil.remove_one_student(student_no, students)
        case '6':
            print('*' * 20, '退出程序', '*' * 20)
            break
        case _:
            print("选择的功能不存咋,请重新选择")
    # 是否退出
    is_exit = input('是否退出【y/n】:')
    if is_exit == 'y':
        break
print('程序结束,欢迎下次使用...')
